package com.practice.question2;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-18 17:11
 */

import java.util.concurrent.TimeUnit;

/**
 * 写一个固定容量的同步容器，拥有put何get方法，以及getCount方法，能够支持两个生产者线程以及10个消费者线程的阻塞调用
 * MyContainer  初次解决方案 存在一个小问题，notifyAll()方法会同时唤醒被阻塞的生产者和消费者
 *
 */
public class Solution {
    public static void main(String[] args) {
        //myContainerSolution();
        myContainer2Solution();
    }
    public static void myContainerSolution(){
        MyContainer<String> container = new MyContainer<>();
        for(int i = 0; i<10; i++){
            new Thread(()->{
                for (int j = 0; j<5; j++){System.out.println(container.get());
                }
            },"c"+i).start();
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i = 0; i<2; i++){
            new Thread(()->{
                for (int j = 0; j<25; j++){
                    container.put(Thread.currentThread().getName()+" "+ j);
                    //System.out.println(Thread.currentThread().getName()+" "+j);
                }
            },"p"+i).start();
        }
    }
    public static void myContainer2Solution(){
        MyContainer2<String> container = new MyContainer2<>();
        for(int i = 0; i<10; i++){
            new Thread(()->{
                for (int j = 0; j<5; j++){System.out.println(container.get());
                }
            },"c"+i).start();
        }
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i = 0; i<2; i++){
            new Thread(()->{
                for (int j = 0; j<25; j++){
                    container.put(Thread.currentThread().getName()+" "+ j);
                    //System.out.println(Thread.currentThread().getName()+" "+j);
                }
            },"p"+i).start();
        }
    }
}
